package com.ruoyan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyan.pojo.Department;
import com.ruoyan.pojo.Employee;
import com.ruoyan.service.DepartmentService;
import com.ruoyan.service.EmployManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.ruoyan.controller
 * @ClassName: EmployManageController
 * @Author: ruoyan1998
 * @CreateTime: 2021/5/16 14:53
 * @Description:
 */
@Controller
public class EmployManageController
{
    @Autowired
    private EmployManageService employManageService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * 显示员工列表信息，查询所有员工信息*/
    @RequestMapping("/emps")
    public String employList(Model model)
    {
//        Collection<Employee> employees = employManageService.getEmployee();

        List<Employee> employeeList = employManageService.queryEmployeeList();

        model.addAttribute("emps",employeeList);

        return "emp/employeeList";
    }

    /**
     * 跳转到添加员工页面，对部门信息先进行查询*/
    @GetMapping("/toAddEmp")
    public String addEmployeePage(Model model)
    {
//        //获取部门的全部信息
//        Collection<Department> departments = departmentService.getDepartments();
//
//        model.addAttribute("departments",departments);

        List<Department> departments = departmentService.queryDepartment();

        model.addAttribute("departments",departments);

        return "emp/add";
    }

    /**
     * 添加员工*/
    @PostMapping("/emp")
    public String addEmployee(Employee employee)
    {
        try
        {
            employManageService.addEmployee(employee);
            return "redirect:/emps";
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "emp/addError";
        }

    }

    /**
     * 跳转到修改员工信息页面*/
    @GetMapping("/emp/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model)
    {
//        //先查出某个员工的信息
//        Employee employee = employManageService.getEmployeeById(id);
//
//        //查出部门信息
//        Collection<Department> departments = departmentService.getDepartments();

        Employee employee = employManageService.queryEmployeeById(id);

        List<Department> departments = departmentService.queryDepartment();

        model.addAttribute("employee",employee);
        model.addAttribute("departments",departments);

//        model.addAttribute("departments",departments);
//        model.addAttribute("employee",employee);

        return "emp/update";
    }

    /**
     * 修改员工信息
     * */
    @PostMapping("/update")
    public String update(Employee employee)
    {
        try
        {
            employManageService.updateEmployee(employee);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "emp/updateError";
        }

        return "redirect:/emps";
    }

    /**
     * 删除员工信息*/
    @GetMapping("/delete/{id}/{did}")
    public String delete(@PathVariable("id")Integer id, @PathVariable("did") int did)
    {
        employManageService.deleteEmployee(id);

        //根据前台传参did获取员工did字段，也就是部门表id值，然后根据did参数查询部门下是否有员工
        List<Employee> employeeList = employManageService.queryEmployeeByDid(did);

        //若部门下无员工，则将该部门的负责人字段更改为无
        if(employeeList.isEmpty())
        {
            Map<String,Object> map = new HashMap<>(2);
            map.put("chargeMan","无");
            map.put("id",did);

            departmentService.updateChargeManToNull(map);
        }

        return "redirect:/emps";
    }

    /**
     * 注销信息*/
    @RequestMapping("/user/logout")
    public String logout(HttpSession session)
    {
        session.invalidate();

        return "index";
    }
}
